//
// Created by Administrator on 2015/11/11 0011.
//

#include "com_jni_zlib_jni_CurlUtils.h"

JNIEnv* env_static;
JNIEXPORT jint JNICALL
JNI_OnLoad(JavaVM *jvm, void *reserved){
    JNIEnv* env = NULL; //注册时在JNIEnv中实现的，所以必须首先获取它
    __android_log_write(ANDROID_LOG_DEBUG, LOG_TAG, "jni onload");
    if ((*jvm)->GetEnv(jvm,(void **)&env, JNI_VERSION_1_6)!=JNI_OK){
        __android_log_write(ANDROID_LOG_ERROR, LOG_TAG, "JNI version not supported");
        return JNI_ERR; /* JNI version not supported */
    }
    env_static = env;
    return JNI_VERSION_1_6;
}

size_t writeFunc(char *data, size_t size, size_t nmemb, char *writerData) {
    jbyteArray jbytedata = NULL;
    size_t len = size * nmemb;
    if (len == 0)
        return 0;
    jbytedata = (*env_static)->NewByteArray(env_static,len);
    (*env_static)->SetByteArrayRegion(env_static,jbytedata,0,len,(jbyte*)data);
    writerData = (char *) malloc(len * sizeof(char));
    stpcpy(writerData, data);
    __android_log_write(ANDROID_LOG_DEBUG, LOG_TAG, writerData);
    return len;
}

JNIEXPORT jstring JNICALL Java_com_jni_zlib_jni_CurlUtils_httpGet
        (JNIEnv *env, jclass obj, jstring url) {
    const char *char_url = (*env)->GetStringUTFChars(env, url, 0);
    __android_log_write(ANDROID_LOG_DEBUG, LOG_TAG, char_url);
    /*
     * curl_easy_init用来初始化一个CURL的指针(有些像返回FILE类型的指针一样). 相应的在调用结束时要用curl_easy_cleanup函数清理.
        一般curl_easy_init意味着一个会话的开始. 它会返回一个easy_handle(CURL*对象), 一般都用在easy系列的函数中.
     * */
    curl_global_init(CURL_GLOBAL_ALL);
    CURL *curl = curl_easy_init();
    char *writerData = "";
    CURLcode code;
    __android_log_write(ANDROID_LOG_DEBUG, LOG_TAG, curl_version());
    //头部清单
    struct curl_slist *headers = NULL;
    headers = curl_slist_append(headers, "Hey-server-hey: how are you?");
    headers = curl_slist_append(headers, "X-silly-content: yes");
    curl_easy_setopt(curl, CURLOPT_URL, char_url);
    //添加头部
    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
    /*
     * //用户读取数据调用
     * curl_easy_setopt(curl, CURLOPT_READFUNCTION, readfunction);
     curl_easy_setopt(curl, CURLOPT_READDATA, readfunction);*/
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeFunc);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &writerData);
    // curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
    //打开gzip
    //curl_easy_setopt(curl, CURLOPT_ACCEPT_ENCODING, "gzip");
    code = curl_easy_perform(curl);
    if (code != 0) {
        __android_log_write(ANDROID_LOG_DEBUG, LOG_TAG, "send request error：");
        __android_log_write(ANDROID_LOG_ERROR, LOG_TAG, (char *) curl_easy_strerror(code));
        return;
    }
    char *ct;
    curl_easy_getinfo(curl, CURLINFO_CONTENT_TYPE, &ct);
    __android_log_write(ANDROID_LOG_DEBUG, LOG_TAG,ct);
    /*这个调用用来结束一个会话.与curl_easy_init配合着用. */
    curl_easy_cleanup(curl);
    (*env)->ReleaseStringUTFChars(env, url, char_url);
    curl_global_cleanup();
    jstring jstring1 = NULL;
    jstring1 = CharTojstring(env, writerData);

    curl_slist_free_all(headers);
    free(writerData);
    return jstring1;
}
